package com.chart.web.controller.chart;

import com.chart.chart.domian.vo.ChartCoinPackageVo;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.core.domain.entity.ChartCoinPackage;
import com.chart.system.service.IChartCoinPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 金币操作controller
 *
 * @author 罗继全
 * 2024/9/10 14:25
 */

@RestController
@RequestMapping("/chart/coin/package")
public class ChartCoinPackageController {

    @Autowired
    private IChartCoinPackageService chartCoinPackageService;

    @GetMapping("/query/list")
    public AjaxResult queryList() {
        List<ChartCoinPackage> list = chartCoinPackageService.list();
        List<ChartCoinPackageVo> voList = list.stream()//将list集合转换为一个流
                .map(this::convertToVo)//对流中的每个元素应用map操作 map操作会将流中的每个元素传递给一个映射函数，并返回一个新流，该流包含映射函数的返回值
                .collect(Collectors.toList());//将流中的元素收集到一个新的列表中
        return AjaxResult.success(voList);
    }

    private ChartCoinPackageVo convertToVo(ChartCoinPackage entity) {
        ChartCoinPackageVo vo = new ChartCoinPackageVo();
        vo.setId(entity.getId());
        vo.setAction(entity.getAction());
        vo.setName(entity.getName());
        vo.setAddCoin(entity.getAddCoin());
        vo.setDeductCoin(entity.getDeductCoin());
        vo.setType(entity.getType());
        vo.setFree(entity.getFree());

        return vo;
    }

}
